package com.cwd.arm.application.service.read;

import com.cwd.arm.application.dto.DictDto;
import com.cwd.arm.application.service.read.facade.DictReadService;
import com.cwd.arm.common.enums.DictTypeEnum;
import com.cwd.arm.common.util.DateUtil;
import com.cwd.arm.infrastructure.dao.DictDao;
import com.cwd.arm.infrastructure.po.DictPo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:数据字典
 * Created on 2021年11月19日
 *
 * @author yan
 */
@Repository
@Primary
public class DictReadServiceImpl implements DictReadService {

    @Autowired
    private DictDao dictDao;

    @Override
    public List<DictDto> selectList(String type) {
        if (DictTypeEnum.FY.getCode().equals(type)) {
            int thisFy = DateUtil.getThisFy();
            return convertToDtoList(DateUtil.getFyList(),thisFy,DictTypeEnum.FY);
        }
        if (DictTypeEnum.MONTH.getCode().equals(type)) {
            int month = DateUtil.getThisMonth();
            return convertToDtoList(DateUtil.getPlanMonthList(),month,DictTypeEnum.MONTH);
        }
        List<DictPo> poList = dictDao.selectList(type);
        return poList.stream().map(po -> (convertToDto(po))).collect(Collectors.toList());
    }

    private DictDto convertToDto(DictPo po) {
        DictDto dto = new DictDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private List<DictDto> convertToDtoList(List<Integer> tList, Integer now,DictTypeEnum typeEnum) {
        return tList.stream()
                .map(t -> (convertToDto(t, now, typeEnum)))
                .collect(Collectors.toList());
    }

    private DictDto convertToDto(Integer t, Integer now,DictTypeEnum typeEnum) {
        DictDto dictDto = new DictDto();
        dictDto.setDictType(typeEnum.getCode());
        dictDto.setDictKey(t.toString());
        dictDto.setDictName(t.toString());
        if (t.equals(now)) {
            dictDto.setSelected(true);
        }
        return dictDto;
    }


}
